package com.wnxy.admin.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wnxy.admin.common.ResponseResult;
import com.wnxy.admin.config.MessageQueueConfig;
import com.wnxy.admin.dto.JChargingPileDto;
import com.wnxy.admin.dto.JChargingStationDto;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnxy.admin.common.handler.ChargingPileStateEnum;
import com.wnxy.admin.dto.ChargingPileDto;
import com.wnxy.admin.dto.ReservationChargingPileDto;
import com.wnxy.admin.entity.ChargingCard;
import com.wnxy.admin.entity.ChargingPile;
import com.wnxy.admin.mapper.ChargingPileMapper;
import com.wnxy.admin.service.IChargingPileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 充电桩 服务实现类
 * </p>
 *
 * @author SJH
 * @since 2023-11-29
 */
@Service
public class ChargingPileServiceImpl extends ServiceImpl<ChargingPileMapper, ChargingPile> implements IChargingPileService {

    private final RabbitTemplate rabbitTemplate;
    private final ChargingPileMapper chargingPileMapper;

    public ChargingPileServiceImpl(ChargingPileMapper chargingPileMapper,RabbitTemplate rabbitTemplate) {
        this.chargingPileMapper = chargingPileMapper;
            this.rabbitTemplate = rabbitTemplate;
    }

    @Override
    public PageInfo<JChargingPileDto> findByPage(String chargingPileName, int pageNum, int pageSize) {
        //1.设置页码和每页大小
        PageHelper.startPage(pageNum, pageSize);
        //2.调用DAO层查询
        List<JChargingPileDto> list = chargingPileMapper.findAll(chargingPileName);
        //3.返回PageInfo对象
        return new PageInfo<>(list);
    }

    @Override
    public int updateById(JChargingPileDto jChargingPileDto) {
        // 如果充电桩状态为故障，则发送消息到消息队列
        if ("故障".equals(jChargingPileDto.getChargingPileState())) {
            LambdaQueryWrapper<JChargingPileDto> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(JChargingPileDto::getChargingPileState, "故障");
        Map<String, Object> map = new HashMap<>();
        map.put("id", jChargingPileDto.getId());
        map.put("level", jChargingPileDto.getMaintenanceState());
        map.put("chargingPileName", jChargingPileDto.getChargingPileName());
        map.put("chargingStationName", jChargingPileDto.getChargingStationName());
        map.put("alarmTime", new Date());
        rabbitTemplate.convertAndSend(MessageQueueConfig.FAULT_QUEUE, map);
        }
        return chargingPileMapper.updateById(jChargingPileDto);
    }
    /*
     * @[预约业务]查询所有电桩信息
     * @author: LQC
     * */
    @Override
    public List<ReservationChargingPileDto> getAllFromReservation() {
        return chargingPileMapper.selectAllFromReservation();
    }

    /*
     * @[预约业务]电桩状态修改接口
     * @author: SJH
     * */
    @Override
    public void updatePileStateById(Long id, String pileState) {
        /* 【注解】 过滤状态(eqState) = ( pileState.equals("预约") ) ? 空闲 : 预约 */
        String eqState = (pileState.equals(ChargingPileStateEnum.PIE_RESERVATION.getCode())) ?
                ChargingPileStateEnum.PIE_DEFAULT.getDiscribe() : ChargingPileStateEnum.PIE_RESERVATION.getDiscribe();
        pileState = ChargingPileStateEnum.getStatus(pileState);

        ChargingPile chargingPile = new ChargingPile();
        UpdateWrapper<ChargingPile> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(ChargingPile::getId, id)
                .set(ChargingPile::getChargingPileState, pileState)
                .eq(ChargingPile::getChargingPileState, eqState);
        chargingPileMapper.update(chargingPile, wrapper);
    }

    @Override
    public void switchPileState(String chargingPileNumber, String pileState) {
        String eqState = (pileState.equals(ChargingPileStateEnum.PIE_WORKING.getCode())) ?
                ChargingPileStateEnum.PIE_DEFAULT.getDiscribe() : ChargingPileStateEnum.PIE_WORKING.getDiscribe();
        pileState = ChargingPileStateEnum.getStatus(pileState);

        ChargingPile chargingPile = new ChargingPile();
        UpdateWrapper<ChargingPile> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(ChargingPile::getChargingPileNumber, chargingPileNumber)
                .set(ChargingPile::getChargingPileState, pileState)
                .eq(ChargingPile::getChargingPileState, eqState);
        chargingPileMapper.update(chargingPile, wrapper);
    }

    @Override
    public List<JChargingPileDto> listDto() {
        return chargingPileMapper.listDto();
    }

}
